<template>
  <div>
    <el-dialog
      title="提示"
      :visible="isShowRole"
      @close="btnCancel"
    >
      <el-checkbox-group v-model="roleIds">
        <el-checkbox v-for="role in roleList" :key="role.id" :label="role.id">
          {{ role.name }}
        </el-checkbox>
      </el-checkbox-group>
      <template #footer>
        <el-button size="small" type="primary" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    isShowRole: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      roleList: [], // 角色列表
      roleIds: [], // 被选中的角色id
      employeeId: '' // 员工id
    }
  },
  async created() {
    const { rows } = await getRoleList({ page: 1, pagesize: 999 })
    this.roleList = rows
    // console.log(this.roleList)
  },
  methods: {
    // 获取被选中的角色id
    async assignRoleID(id) {
      this.employeeId = id
      const { roleIds } = await getUserDetailById(id)
      this.roleIds = roleIds
    },
    // 确定
    async btnOK() {
      await assignRoles({
        id: this.employeeId,
        roleIds: this.roleIds
      })
      this.$message.success('操作成功')
      this.$emit('update:isShowRole', false)
    },
    // 取消
    btnCancel() {
      this.roleIds = []
      this.$emit('update:isShowRole', false)
    }
  }
}
</script>

<style>

</style>
